package com.qumitech.visualnovel.component.amqp;

import com.qumitech.visualnovel.component.util.CustomWebSocket;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.ByteArrayInputStream;
import java.io.ObjectInputStream;

/**
 * @Author :  ZhChNing
 * @Descriptiom :
 * @Date Created in 2019/3/15 15:23
 */
//@Component
@RabbitListener(queues = AmqpConstant.weChat_Alarm)
public class AmqpReceiver {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired
    private CustomWebSocket customWebSocket;

    @RabbitHandler
    public void process(String message) throws Exception {
        try {
            CustomWebSocket.sendInfo(message);
            logger.info("success to receiver amqp message, registerExchange name:{}, message:{}", AmqpConstant.weChat_Alarm, message);
        } catch (Exception e) {
            logger.info("CustomWebSocket 信息发送失败:{}", e.getMessage());
        }

    }

    public Object getObjectFromBytes(byte[] objBytes) throws Exception {
        if (objBytes == null || objBytes.length == 0) {
            return null;
        }
        ByteArrayInputStream bi = new ByteArrayInputStream(objBytes);
        ObjectInputStream oi = new ObjectInputStream(bi);
        return oi.readObject();
    }

}
